package cn.edu.zjut.medlinebackend.service.impl;
import cn.edu.zjut.medlinebackend.mapper.GroupMapper;
import cn.edu.zjut.medlinebackend.mapper.MembersMapper;
import cn.edu.zjut.medlinebackend.mapper.NotificationMapper;
import cn.edu.zjut.medlinebackend.model.Members;
import cn.edu.zjut.medlinebackend.model.Notification;
import cn.edu.zjut.medlinebackend.service.GroupService;
import cn.edu.zjut.medlinebackend.model.Group;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GroupServiceImpl  extends ServiceImpl<GroupMapper, Group>
        implements GroupService {
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private MembersMapper membersMapper;
    @Resource
    private NotificationMapper notificationMapper;
    @Override
    public boolean creatGroup(Group group) {
        int insert = groupMapper.insert(group);
        return insert > 0;
    }

    @Override
    public List<Group> searchGroup(String groupname) {
        return groupMapper.searchGroup(groupname);
    }

    @Override
    public List<Group> getAllGroup(String username) {
        List<Group> groups=new ArrayList<>();
        List<String> joingroups=membersMapper.getAllByUsername(username);
        for (String groupname:joingroups) {
            groups.add(groupMapper.getGroupByGroupname(groupname));
        }
        return groups;
    }

    @Override
    public Group searchGroupByName(String groupname) {
        return groupMapper.getGroupByGroupname(groupname);
    }


    @Override
    public boolean deleteGroup(String groupname, String username) {
        Group group = groupMapper.getGroupByGroupname(groupname);
        if(group.getCreator().equals(username)) {
            //    **********************
            List<Members> members = membersMapper.getMembers(groupname);
            for(Members member:members){
                Notification notification = new Notification();
                membersMapper.deleteMember(groupname,member.getUsername());
                notification.setRecipient(member.getUsername());
                notification.setSender(group.getCreator());
                notification.setReferencegroup(groupname);
                notification.setType("解散群组");
                notification.setText("群主:" + group.getCreator() + " 解散群组:" + groupname);
                notification.setStatus("未读");
                notificationMapper.insert(notification);
            }
            //   **************************************
            groupMapper.deleteByGroupname(groupname);
            return true;
        }
        else return false;
    }

    @Override
    public boolean joinGroup(String username, String groupname) {
        List<Members> members=membersMapper.getMembers(groupname);
        for(Members member:members){ //判断是否是成员
            if(member.getUsername().equals(username)) return false;
        }
        Group group = groupMapper.getGroupByGroupname(groupname);
        if(group.getCreator().equals(username)) return false; //判断是否为创建者
        //    **********************
        Notification notification = new Notification();
        notification.setRecipient(groupMapper.getGroupByGroupname(groupname).getCreator());
        notification.setSender(username);
        notification.setReferencegroup(groupname);
        notification.setType("加入群组");
        notification.setText("成员:" + username + "加入群组:" + groupname);
        notification.setStatus("未读");
        notificationMapper.insert(notification);
        //**************************************
        Members member=new Members();
        member.setUsername(username);
        member.setGroupname(groupname);
        membersMapper.insert(member);
        return true;
    }

    @Override
    public boolean exit(String username, String groupname) {
        List<Members> members=membersMapper.getMembers(groupname);
        for(Members member:members){ //判断是否是成员
            if(member.getUsername().equals(username)) {
                membersMapper.deleteMember(groupname, username);
                //    **********************
                Notification notification = new Notification();
                notification.setRecipient(groupMapper.getGroupByGroupname(groupname).getCreator());
                notification.setSender(username);
                notification.setReferencegroup(groupname);
                notification.setType("退出群组");
                notification.setText("成员:" + username + "退出群组:" + groupname);
                notification.setStatus("未读");
                notificationMapper.insert(notification);
                //    **************************************
                return true;
            }
        }
        return false;
    }


}
